package com.tianqu.tms.www.car.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dao.TmsConfigureDaoimp;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * mufeiyu
 */
public class CarLicensePlateListAjaxAction extends TmsAjaxAction {
	private Logger logger = Logger.getLogger(CarLicensePlateListAjaxAction.class);

	@Override
	public void process(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws TmsException {

		logger.debug(this.getClass().getName() + "#execute.");
		String query = request.getParameter("query");
		String type = request.getParameter("type");

		List list = new ArrayList();
		Map map = null;
		if("1".equals(type)){
			TransferAcceptedInfo acceptedInfo = TransferAcceptedInfo.getInstance();
			list.addAll(acceptedInfo.findLicensePlateList(query));
		}else if("2".equals(type)){
			Car car = Car.getInstance();
			List<Car> carList = car.findLicensePlateListByRegisterStatus(query);
			for (Car carInfo : carList) {
				map = new HashMap();
				map.put("value", carInfo.getLicensePlate());
				map.put("data", carInfo.getcId());
				list.add(map);
			}
		} else if("3".equals(type)){
			TmsConfigureDaoimp tmsConfigureDao = (TmsConfigureDaoimp) TmsConfigureDaoimp.getInstance(TmsDao.TMS_CONFIGURE);
			String defLicensePlate = tmsConfigureDao.getConfig("defLicensePlate");
			map = new HashMap();
			map.put("value", defLicensePlate);
			map.put("data", "0");
			list.add(map);
		} else{
			Car car = Car.getInstance();
			List<Car> carList = car.findLicensePlateList(query);
			for (Car carInfo : carList) {
				map = new HashMap();
				map.put("value", carInfo.getLicensePlate());
				map.put("data", carInfo.getcId());
				list.add(map);
			}
		}
		
		JSONArray jsonArray = JSONArray.fromObject(JSONArray.fromObject(list));
		JSONObject json = new JSONObject();
		json.put("query", query);
		json.put("suggestions", jsonArray);

		responseValues(response, json.toString(), TEXT);

		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}
}
